package org.devocean.groupbuying.domain;

import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MapKeyColumn;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;

@SuppressWarnings("serial")
@Entity
public class Article implements Serializable {

	@Id
	@GeneratedValue
	private Integer articleID;

	private String lang;

	@Column(columnDefinition = "TEXT")
	private String shorte;

	@OneToMany(mappedBy = "article",fetch = FetchType.LAZY,cascade = CascadeType.ALL)
	@OrderBy("commentDate DESC")
	private Set<Comment> comments = new HashSet<Comment>();

	private String title;

	@Column(columnDefinition = "TEXT")
	private String body;

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "userID", referencedColumnName = "userID", nullable = false)
	private User owner;

	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.DETACH)
	@JoinColumn(name = "articlesLocalizationID", referencedColumnName = "articlesLocalizationID", nullable = false)
	private ArticlesLocalization articlesLocalization;

	@ElementCollection
	@MapKeyColumn(name = "additional_contents_key")
	@Column(name = "additional_contents")
	@CollectionTable(name = "article_additional_contents", joinColumns = @JoinColumn(name = "article"))
	private Map<String, String> additionalContents = new HashMap<String, String>();

	public ArticlesLocalization getArticlesLocalization() {
		return articlesLocalization;
	}

	public void setArticlesLocalization(
			ArticlesLocalization articlesLocalization) {
		this.articlesLocalization = articlesLocalization;
	}

	public Integer getArticleID() {
		return articleID;
	}

	public void setArticleID(Integer articleID) {
		this.articleID = articleID;
	}

	public String getShorte() {
		return shorte;
	}

	public void setShorte(String shorte) {
		this.shorte = shorte;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getBody() {
		return body;
	}

	public void setBody(String body) {
		this.body = body;
	}

	public String getLang() {
		return lang;
	}

	public void setLang(String lang) {
		this.lang = lang;
	}

	public Map<String, String> getAdditionalContents() {
		return additionalContents;
	}

	public void setAdditionalContents(Map<String, String> additionalContents) {
		this.additionalContents = additionalContents;
	}

	public User getOwner() {
		return owner;
	}

	public void setOwner(User owner) {
		this.owner = owner;
	}

	public Set<Comment> getComments() {
		return comments;
	}

	public void setComments(Set<Comment> comments) {
		this.comments = comments;
	}

	@Override
	public String toString() {
		return "Article [articleID=" + articleID + ", lang=" + lang
				+ ", shorte=" + shorte + ", title=" + title + ", body=" + body
				+ "]";
	}

}
